<template>
  <div ref="imageWrapper">
    <a-card title="查询区域" class="search">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form-model ref="ruleForm" :model="queryParam" :rules="rules" layout="inline" @keyup.enter.native="onSearch">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="账套/核算单元" prop="accName">
                <a-input v-model="queryParam.accName" readOnly placeholder="请选择" @click.native="openOrganModel" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-model-item label="审计时间" prop="arrayTime">
                <a-range-picker v-model="queryParam.arrayTime" format="yyyy-MM-DD" value-format="yyyy-MM-DD"
                  @change="handleDatechange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8" :offset="6">
              <span style="float: right; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" icon="search" class="yellow_search_btn" @click="onSearch">查询</a-button>
                <a-button icon="reload" class="clear_btn" @click="searchReset">清除条件</a-button>
                <a-button type="danger" :loading="ydLoading" icon="arrow-right" style="margin-left: 18px"
                  @click="handleToYD(false)">转疑点</a-button>
                <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                  返回
                </a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card v-if="modelData.length != 0" class="img_css" style="margin-top: 20px">
      <div class="Exceed_funds_map">
        <div class="title">
          <a-tooltip overlay-class-name="bgc_tooltip" placement="topRight">
            <template slot="title">
              <span>{{ modeltoop.mxms }}</span>
            </template>
            <span @click="getModelData">
              {{ modeltoop.modelName || '应收账款管理模型' }}
              <a-icon type="strikethrough" @click.stop="showImg" />
            </span>
          </a-tooltip>
        </div>
        <div class="midline">
          <div class="line_v" />
          <div class="line_h" />
        </div>
        <div class="content_box">
          <div v-for="(item, index) in modelData" :key="index" class="content_item">
            <div class="line_v" />
            <div class="step3_left_left" :style="{ color: item.modifyColor }" :class="{ active: index === activeIndex }"
              @click="getModelItem(index, item)">
              <div class="title">
                {{ item.modelName }}
              </div>
              <div class="count">
                {{ item.extraNum || 0 }}
              </div>
              <a-icon type="caret-down" class="logo" />
            </div>
          </div>
        </div>
      </div>
    </a-card>
    <a-card style="margin-top: 20px">
      <a-table :columns="columns" :data-source="dataSource" row-key="id" :bordered="true" :loading="loading"
        class="antableClass" :pagination="ipagination" :custom-row="handleClickRow" @change="handleTableChange">
        <template v-slot:unitName="text">
          <j-ellipsis :value="text" :length="20"></j-ellipsis>
        </template>
        <template v-slot:accName="text">
          <j-ellipsis :value="text" :length="20"></j-ellipsis>
        </template>
        <template v-slot:relatedPartyName="text">
          <j-ellipsis :value="text" :length="15"></j-ellipsis>
        </template>
        <template v-slot:proName="text">
          <j-ellipsis :value="text" :length="20"></j-ellipsis>
        </template>
        <span slot="action" slot-scope="text, record">
          <a style="color: red" @click.stop="handleToYD(record)">转疑点</a>
        </span>
      </a-table>
    </a-card>
    <CheckOrganModal ref="departModal" @ok="updateQueryParam" />
    <BusinessChart ref="businesschart" :flowimg="flowimg" />
    <YDModel ref="YDModel" :router-data="$route.path" />
    <CheckAndEditModel ref="modalForm" />
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import CheckOrganModal from '../../../sjxmzygl/yzzx/testcomponent/CheckOrganModal'
import CheckAndEditModel from './CheckAndEditModel'
import BusinessChart from './BusinessChart'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from '@/views/sj/components/YDModel'
import columns from './indexColumns'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
export default {
  components: { CheckOrganModal, CheckAndEditModel, BusinessChart, YDModel },
  mixins: [JeecgListMixin, base64toImg, jumpConfigMixin],
  data() {
    return {
      queryParam: {
        accName: '',
        unitId: '',
        startTime: '',
        endTime: '',
        extraStartTime: '',
        extraEndTime: '',
        num: '',
        arrayTime: [],
      },
      superParam: {
        id: 'd146af23757f4807c4b05da0b1b11c7b',
        extraFlag: true,
      },
      modelData: [],
      url: {
        list: '/sjmx/queryYszkglYear',
        model: '/dataModel/queryDataModelTemplateByExtra',
        details: '/sjmx/queryYszkgl',
      },
      isBackButton: false,
      modeltoop: [],
      activeIndex: null,
      rules: {
        accName: [{ required: true, message: '请选择账套/核算单元' }],
        arrayTime: [{ required: true, message: '请选择审计时间' }],
      },
      columns: columns(),
      disableMixinCreated: true,
    }
  },
  computed: {
    flowimg() {
      const url = `${window._CONFIG['domianURL']}/sys/common/static/${this.modeltoop.mxUrl}`
      console.log('url', url)
      return url
    },
  },
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.query).length) {
          this.disableMixinCreated = true
          let activeIndex = ((to.query.activeIndex && Number.parseInt(to.query.activeIndex)) || 0) - 1
          to.query.activeIndex && delete to.query.activeIndex
          this.queryParam = Object.assign(this.queryParam, to.query)
          this.queryParam.arrayTime = to.query.arrayTime.split(',')
          this.$nextTick(() => {
            this.onSearch().then((res) => {
              console.log(activeIndex)
              if (activeIndex >= 0) {
                this.getModelItem(activeIndex, this.modelData[activeIndex])
              }
            })
          })
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          let { accName, unitId } = to.params
          this.queryParam.accName = accName
          this.queryParam.unitId = unitId
          let { endDate, startDate } = JSON.parse(to.params.returnRecord)
          this.queryParam.endTime = endDate
          this.queryParam.startTime = startDate
          this.queryParam.arrayTime = [ startDate,endDate]
          this.$nextTick(() => {
            this.onSearch()
          })
          this.isBackButton = true
          this.returnRecord = to.params.returnRecord
        }
      },
      immediate: true,
    },
  },
  created() { },
  methods: {
    searchReset() {
      this.queryParam = this.$options.data().queryParam
      this.dataSource = []
      this.modelData = []
    },
    handleClickRow(record, index) {
      const _this = this
      return {
        on: {
          click: () => {
            console.log(111, record)
            _this.$refs.modalForm.queryParam = _this.queryParam
            _this.handleCheck(record, '详情')
          },
        },
      }
    },
    onSearch() {
      return new Promise((reslove, reject) => {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            await this.getModel()
            await this.getModelData()
            reslove()
          } else {
            reject()
          }
        })
      })
    },
    getModelItem(index, { modelName, id }) {
      console.log(index)
      this.activeIndex = index
      const num = modelName.replace(/[^0-9]/gi, '')
      this.queryParam.num = num
      this.loadData(1)
      // const params = Object.assign({}, this.queryParam, this.superParam, { num: num })
      // getAction(this.url.list, params).then((res) => {
      //   this.dataSource = res.result.records
      //   if (res.result.total) {
      //     this.ipagination.total = res.result.total
      //   } else {
      //     this.ipagination.total = 0
      //   }
      // })
    },
    getModel() {
      return new Promise((reslove, reject) => {
        this.activeIndex = null
        const params = this.getQueryParams()
        getAction(this.url.model, params).then((res) => {
          if (res.success) {
            const data = res.result.records || res.result
            this.modeltoop = data.shift()
            console.log(this.modeltoop)
            this.modelData = data
            reslove()
          } else {
            if (res.code === 510) {
              this.$message.warning(res.message)
            }
            reject()
          }
        })
      })
    },
    getModelData() {
      return new Promise((reslove, reject) => {
        this.activeIndex = null
        this.queryParam.num = null
        const params = this.getQueryParams()
        getAction(this.url.details, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            reslove()
          } else {
            if (res.code === 510) {
              this.$message.warning(res.message)
            }
            reject()
          }
        })
      })
    },
    handleTableChange(pagination, filters, sorter) {
      // 分页、排序、筛选变化时触发
      // TODO 筛选
      console.log(pagination)
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = sorter.order == 'ascend' ? 'asc' : 'desc'
      }
      this.ipagination = pagination
      this.getModelData()
    },
    showImg() {
      this.$refs.businesschart.show()
      //    this.$message.warning('请先配置查询条件')
    },
    openOrganModel() {
      this.$refs.departModal.edit()
    },
    updateQueryParam(data, orgId) {
      // console.log(data, 'daat')
      let str1 = ''
      let str2 = ''
      let str3 = ''
      data.map((item) => {
        // TODO 账套～～～
        str1 += item.unitId + ','
        str2 += item.accName + ','
        str3 += item.accCode + ','
      })
      if (str1.length > 0) {
        str1 = str1.substr(0, str1.length - 1)
      }
      if (str2.length > 0) {
        str2 = str2.substr(0, str2.length - 1)
      }
      if (str3.length > 0) {
        str3 = str3.substr(0, str3.length - 1)
      }
      this.queryParam.accCode = str3
      this.queryParam.unitId = str1
      this.queryParam.extraId = str1
      this.queryParam.accName = str2
      this.queryParam.orgId = orgId
    },
    handleToYD(record) {
      if (this.dataSource.length) {
        const { startTime = '', endTime = '' } = this.queryParam
        let ydnr
        const time = `【${startTime}～${endTime}】【应收账款管理模型】`
        let param = Object.assign({}, this.queryParam, { activeIndex: this.activeIndex })
        console.log('param---->', param)
        this.addYd(param, ydnr, time)
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
    handleDatechange(dates) {
      this.queryParam.startTime = dates[0]
      this.queryParam.endTime = dates[1]
      this.queryParam.extraStartTime = dates[0]
      this.queryParam.extraEndTime = dates[1]
    },
  },
}
</script>
<style lang="less">
.bgc_tooltip {
  .ant-tooltip-inner {
    // 这里是框框
    border-radius: 20px;
    color: #333;
    width: 400px;
    padding: 10px 15px;
    background-color: rgba(255, 255, 255, 0.85) !important;
  }

  .ant-tooltip-arrow::before {
    // 这里是小三角形
    background-color: #fff !important;
  }
}
</style>
<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';

.img_css /deep/ .ant-card-body {
  padding-top: 7px;
}

/deep/ .ant-table-thead {
  tr {
    th {
      text-align: center !important;
    }
  }
}

.antableClass {
  /deep/ .ant-table-row {
    cursor: pointer;
  }
}

.Exceed_funds_map {
  width: 100%;
  position: relative;

  .title {
    // width: 350px;
    height: 50px;
    text-align: center;
    margin: 0 auto;

    padding-top: 7px;

    span {
      border-bottom-left-radius: 30px;
      border-bottom-right-radius: 30px;
      font-size: 22px;
      color: white;
      padding: 10px 35px 15px;
      cursor: pointer;
      background-color: rgb(1, 69, 142);
    }
  }

  .midline {
    margin: 0 auto;
    width: 100%;
    height: 40px;

    .line_v {
      margin: 0 auto;
      width: 4px;
      height: 36px;
      border-left: 2px dotted rgb(1, 69, 142);
    }

    .line_h {
      margin: 0 auto;
      width: 66.5%;
      border-bottom: 2px dotted rgb(1, 69, 142);
    }
  }

  .content_box {
    width: 100%;
    display: flex;
    justify-content: space-around;

    .content_item {
      display: flex;
      flex-direction: column;
      justify-content: flex-start;

      .line_v {
        margin: 0 auto;
        height: 15px;
        width: 4px;
        border-left: 2px dotted rgb(1, 69, 142);
      }

      display: flex;
      justify-content: space-around;
      align-items: center;

      .step3_left_left {
        // flex-shrink:1;
        width: 200px;
        height: 70px;
        border: 2px solid #000e89;
        font-size: 16px;
        font-weight: 700;
        color: white;
        text-align: center;
        cursor: pointer;

        .title {
          height: 35px;
          line-height: 20px;
          background-color: rgb(1, 69, 142);
        }

        .count {
          line-height: 30px;
          height: 31px;
          color: rgb(1, 69, 142);
        }

        .logo {
          position: relative;
          top: -5px;
          color: #fff;
        }
      }

      .step3_left_left {
        margin-left: 10px;
      }
    }
  }

  .active {
    .title {
      background-color: rgb(236, 149, 54) !important;
      height: 30px;
      padding-bottom: 10px !important;
    }

    .count {
      color: orange !important;
      background-color: rgb(253, 245, 236);
    }

    .logo {
      color: rgb(236, 149, 54) !important;
    }

    border: 2px solid #ec9536 !important;
  }
}
</style>
